package com.open.lock.config;

import com.open.lock.enums.LockModel;
import com.open.lock.enums.Model;
import org.redisson.config.ReadMode;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.util.ArrayList;
import java.util.List;

/**
 * 分布式锁配置文件
 *
 * @author zhang kaichao
 */
@ConfigurationProperties(prefix = "redisson")
public class RedissonProperties {

    /**
     * 前缀
     */
    private String prefix = "cloud_lock";

    /**
     * 默认单节点模式
     */
    private Model model = Model.SINGLE;
    /**
     * 编码：默认值org.redisson.codec.JsonJacksonCodec
     */
    private String codec = "org.redisson.codec.JsonJacksonCodec";

    /**
     * 监控锁看门狗，检查锁的超时时间 默认30s
     */
    private Long lockWatchdogTimeout = 30000L;

    /**
     * 锁的模式 如果不设置 单个key默认可重入锁 多个key默认联锁
     */
    private LockModel lockModel;

    /**
     * 等待加锁超时时间 -1一直等待
     */
    private Long attemptTimeout = 10000L;

    /**
     * 数据缓存时间 默认30分钟
     */
    private Long dataValidTime = 1000 * 60 * 30L;

    /**
     * 密码
     */
    private String password;
    /**
     * 客户端名称
     */
    private String clientName;

    /**
     * 连接超时，单位：毫秒  默认值：10000
     */
    private Integer connectTimeout = 10000;

    /**
     * 命令等待超时 默认值：3000  单位：毫秒
     */
    private Integer timeout = 3000;

    @NestedConfigurationProperty
    private SingleProperties single = new SingleProperties();

    @NestedConfigurationProperty
    private MultipleProperties multiple = new MultipleProperties();

    public Model getModel() {
        return model;
    }

    public void setModel(Model model) {
        this.model = model;
    }

    public String getCodec() {
        return codec;
    }

    public void setCodec(String codec) {
        this.codec = codec;
    }

    public Long getLockWatchdogTimeout() {
        return lockWatchdogTimeout;
    }

    public void setLockWatchdogTimeout(Long lockWatchdogTimeout) {
        this.lockWatchdogTimeout = lockWatchdogTimeout;
    }

    public LockModel getLockModel() {
        return lockModel;
    }

    public void setLockModel(LockModel lockModel) {
        this.lockModel = lockModel;
    }

    public Long getAttemptTimeout() {
        return attemptTimeout;
    }

    public void setAttemptTimeout(Long attemptTimeout) {
        this.attemptTimeout = attemptTimeout;
    }

    public Long getDataValidTime() {
        return dataValidTime;
    }

    public void setDataValidTime(Long dataValidTime) {
        this.dataValidTime = dataValidTime;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public Integer getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public Integer getTimeout() {
        return timeout;
    }

    public void setTimeout(Integer timeout) {
        this.timeout = timeout;
    }

    public SingleProperties getSingle() {
        return single;
    }

    public void setSingle(SingleProperties single) {
        this.single = single;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public MultipleProperties getMultiple() {
        return multiple;
    }

    public void setMultiple(MultipleProperties multiple) {
        this.multiple = multiple;
    }

    public static class SingleProperties {
        /**
         * 地址
         */
        private String address;
        /**
         * 数据库编号 默认值：0
         */
        private Integer database = 0;

        /**
         * 连接池大小 默认64
         */
        private Integer connectionPoolSize = 64;

        /**
         * 最小空闲连接数  默认值：32
         */
        private Integer connectionMinimumIdleSize = 32;

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public Integer getDatabase() {
            return database;
        }

        public void setDatabase(Integer database) {
            this.database = database;
        }

        public Integer getConnectionPoolSize() {
            return connectionPoolSize;
        }

        public void setConnectionPoolSize(Integer connectionPoolSize) {
            this.connectionPoolSize = connectionPoolSize;
        }

        public Integer getConnectionMinimumIdleSize() {
            return connectionMinimumIdleSize;
        }

        public void setConnectionMinimumIdleSize(Integer connectionMinimumIdleSize) {
            this.connectionMinimumIdleSize = connectionMinimumIdleSize;
        }
    }

    public static class MultipleProperties {
        /**
         * 集群：集群扫描间隔时间，单位是毫秒,默认1000
         */
        private Integer scanInterval = 1000;
        /**
         * 集群、哨兵：读取操作的负载均衡模式 默认值： SLAVE（只在从服务节点里读取） MASTER_SLAVE、MASTER
         */
        private ReadMode readMode = ReadMode.SLAVE;
        /**
         * 集群：负载均衡算法类的选择 默认值：轮询调度算法，可选（WeightedRoundRobinBalancer）权重轮询调度算法，（RandomLoadBalancer）随机调度算法
         */
        private String loadBalancer = "org.redisson.connection.balancer.RoundRobinLoadBalancer";

        private List<String> node = new ArrayList<String>();
        /**
         * 哨兵模式,主从
         */
        private Integer database = 0;
        /**
         * 哨兵模式  主服务器的名称,主服务器的名称是哨兵进程中用来监测主从服务切换情况的
         */
        private String masterName;

        /**
         * 从节点最小空闲连接数
         */
        private Integer slaveConnectionMinimumIdleSize = 32;

        /**
         * 从节点连接池大小
         */
        private Integer slaveConnectionPoolSize = 64;

        /**
         * 主节点最小空闲连接数
         */
        private Integer masterConnectionMinimumIdleSize = 32;

        /**
         * 主节点连接池大小
         */
        private Integer masterConnectionPoolSize = 64;

        public Integer getScanInterval() {
            return scanInterval;
        }

        public void setScanInterval(Integer scanInterval) {
            this.scanInterval = scanInterval;
        }

        public ReadMode getReadMode() {
            return readMode;
        }

        public void setReadMode(ReadMode readMode) {
            this.readMode = readMode;
        }

        public String getLoadBalancer() {
            return loadBalancer;
        }

        public void setLoadBalancer(String loadBalancer) {
            this.loadBalancer = loadBalancer;
        }

        public List<String> getNode() {
            return node;
        }

        public void setNode(List<String> node) {
            this.node = node;
        }

        public Integer getDatabase() {
            return database;
        }

        public void setDatabase(Integer database) {
            this.database = database;
        }

        public String getMasterName() {
            return masterName;
        }

        public void setMasterName(String masterName) {
            this.masterName = masterName;
        }

        public Integer getSlaveConnectionMinimumIdleSize() {
            return slaveConnectionMinimumIdleSize;
        }

        public void setSlaveConnectionMinimumIdleSize(Integer slaveConnectionMinimumIdleSize) {
            this.slaveConnectionMinimumIdleSize = slaveConnectionMinimumIdleSize;
        }

        public Integer getSlaveConnectionPoolSize() {
            return slaveConnectionPoolSize;
        }

        public void setSlaveConnectionPoolSize(Integer slaveConnectionPoolSize) {
            this.slaveConnectionPoolSize = slaveConnectionPoolSize;
        }

        public Integer getMasterConnectionMinimumIdleSize() {
            return masterConnectionMinimumIdleSize;
        }

        public void setMasterConnectionMinimumIdleSize(Integer masterConnectionMinimumIdleSize) {
            this.masterConnectionMinimumIdleSize = masterConnectionMinimumIdleSize;
        }

        public Integer getMasterConnectionPoolSize() {
            return masterConnectionPoolSize;
        }

        public void setMasterConnectionPoolSize(Integer masterConnectionPoolSize) {
            this.masterConnectionPoolSize = masterConnectionPoolSize;
        }

    }

}
